/*********************************************************************************************************
Class Name: NMRcidredirController 
* Author: Accenture 
* Date: 20-March-2012  
* Requirement/Project Name: NMR 
* Requirement Description: Controller class to redirect the User to Account if Customer ID is found
                            else redirect user to Home Page.
*********************************************************************************************************/

public class NMRcidredirController {
    public string custId{get;set;} // To store the CSS_CustomerId__c being passed from AdSell.
    public list<account> retrieveAccountList = new List<Account>();// Matching Account in SFDC Based on CSS_CustomerId__c    
    public ID recordTypeIdAccount = RecordTypeHelper.GetRecordTypeIdByDevName('Account', 'CSS_Customer'); // Store RecordTypeId of NMR Account.
    public Id accountRecordId; // To store SFDC ID of the matching Account Record.
    
    public NMRcidredirController(ApexPages.StandardController controller) {
        //recievedAccountRecord = (Account)controller.getRecord();
        custId = ApexPages.currentPage().getParameters().get('cid'); // fetch 'cid' value being passed from AdSell and store it in a variable
    }
   // Code to fetch the Account in Salesforce  based on CSS Customer ID value that's being fetched from URL.
   public PageReference retrieveAccount(){
          retrieveAccountList = [Select id from Account where RecordTypeId = :recordTypeIdAccount and CSS_CustomerID__c = :custId ] ;
          PageReference pageRef;
          if(retrieveAccountList.size()>0 ){
               accountRecordId = retrieveAccountList[0].id;
                              pageRef = new PageReference('/'+accountRecordId );
                       
           }
           else{         
               // if CSS Customer ID is not found in SFDC, redirect to Home page.      
               pageRef = new PageReference('/home/home.jsp');
           }
           return pageRef;
      } 
      
    static testMethod void NMRcidredirControllerTest() { 
       
    ID accountRecordTypeId = RecordTypeHelper.GetRecordTypeID('Account','CSS Customer');    
        Account acct = new Account();
        acct.Name = 'TestAccount';
        acct.RecordTypeId = accountRecordTypeId;
        acct.Phone = '9539539539';
        acct.CSS_CustomerID__c = '555';
        insert acct;
            PageReference pageRef = Page.cidredir;      
            Test.setCurrentPage(pageRef);
            ApexPages.StandardController ctrl = new ApexPages.StandardController(acct);
            ApexPages.currentPage().getParameters().put('cid', '555');
            
            NMRcidredirController controller = new NMRcidredirController(ctrl);
            controller.custId = '555';
            controller.retrieveAccount();
                }
  }